//
//  MyCouponViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/31.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MyCouponViewController.h"
#import "WSFSlideTitlesView.h"
#import "MyCouponModel.h"
#import "MyCouponListCell.h"
#import "MDDNoCouponView.h" // 没有优惠券的视图

@interface MyCouponViewController ()<WSFSlideTitlesViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *selfTableView;
@property (nonatomic, strong) WSFSlideTitlesView *titlesTopView;
@property (nonatomic, strong) MDDNoCouponView *emptyView;

@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation MyCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    self.view.backgroundColor = [UIColor whiteColor];
    self.selfTableView.backgroundColor = [UIColor whiteColor];
    mNavitationTitle(@"我的优惠券");
    [self.view addSubview:self.titlesTopView];
    self.dataArr = [[NSMutableArray alloc]init];
    [self loadMyCouponListWithType:@"0"];
    self.emptyView = [[[NSBundle mainBundle]loadNibNamed:@"MDDNoCouponView" owner:nil options:nil]firstObject];
    self.emptyView.frame = CGRectMake(0, 200, KScreenWidth, 300);
    [self.view addSubview:self.emptyView];
    self.emptyView.hidden = YES;
    
}

#pragma mark -
#pragma mark - **************** tableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MyCouponListCell * itemCell = mGetTableViewCell(@"myCouponListCell");
    MyCouponModel * itemModel = self.dataArr[indexPath.section];

    itemCell.priceLabel.text = [NSString stringWithFormat:@"%@",itemModel.money];
    itemCell.nameLabel.text = itemModel.couponName;
    itemCell.timeLabel.text = [NSString stringWithFormat:@"%@-%@",itemModel.yxqq,itemModel.yxqz];
    
    //抵扣券状态：-1已过期；0未使用；1已使用； ,
    itemCell.typeName = itemModel.state;
    
    if ([itemModel.state isEqualToString:@"0"]) {
        itemCell.guoqiView.hidden = YES;
        [itemCell.shiyongBtn setTitle:@"立即使用" forState:0];
    }else if ([itemModel.state isEqualToString:@"1"]) {
        itemCell.guoqiView.hidden = YES;
        [itemCell.shiyongBtn setTitle:@"已使用" forState:0];
    }else{//-1 已过期
        itemCell.guoqiView.hidden = NO;
        itemCell.guoqiView.alpha=0.4;
        [itemCell.shiyongBtn setTitle:@"已过期" forState:0];
    }
    
    if (itemModel.full.integerValue == 0) {
        itemCell.tiaoJianLabel.text = @"无限制";
    }else{
         itemCell.tiaoJianLabel.text = [NSString stringWithFormat:@"满%@元使用",itemModel.full];
    }
    itemCell.btnActionBlock = ^{
        self.tabBarController.selectedIndex = 1;
        [self.navigationController popViewControllerAnimated:NO];
    };
    return itemCell;
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCouponModel * itemModel = self.dataArr[indexPath.section];
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要该优惠券?删除后无法恢复!" preferredStyle:1];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
            setDickeyobj(parameters, itemModel.cid, @"cardId");
            setDickeyobj(parameters, @"1", @"add");
            [PPNetworkHelper POST:app_coupon_deleteMyCoupon parameters:parameters success:^(id responseObject) {
                if (YJPPSUCCESS(responseObject)) {
                    [self.dataArr removeObject:itemModel];
                }else{
                    
                }
                [self.selfTableView reloadData];
                if (self.dataArr.count==0 ) {
                    self.emptyView.hidden =NO;
                }else{
                    self.emptyView.hidden =YES;
                }
            } failure:^(NSError *error) {
                
            } showLoading:nil];
            
        }];
        
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        [alert addAction:okAction];
        [alert addAction:cancel];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}
-(void)loadMyCouponListWithType:(NSString *)type
{
    NSMutableDictionary * parameters = [[NSMutableDictionary alloc]init];
    setDickeyobj(parameters, type, @"type");
    setDickeyobj(parameters, @"1", @"add");
    [PPNetworkHelper POST:app_coupon_myCouponList parameters:parameters success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            for (NSDictionary * infoDic in responseObject[@"data"]) {
                MyCouponModel * itemModel = [[MyCouponModel alloc]init];
                [itemModel setValuesForKeysWithDictionary:infoDic];
                [self.dataArr addObject:itemModel];
            }
        }else{
            
        }
        [self.selfTableView reloadData];
        if (self.dataArr.count==0 ) {
            self.emptyView.hidden =NO;
        }else{
            self.emptyView.hidden =YES;
        }
    } failure:^(NSError *error) {
        
    } showLoading:nil];
}
#pragma makr -lozyLoad
-(WSFSlideTitlesView *)titlesTopView{
    if (!_titlesTopView) {
        WSFSlideTitlesViewSetting *titlesSetting = [[WSFSlideTitlesViewSetting alloc] init];
        titlesSetting.titlesArr = @[ @"未使用",@"已使用",@"已过期"];
        titlesSetting.frame = CGRectMake(0, getRectNavAndStatusHight, mScreenWidth, 44);
        titlesSetting.lineHeight = 1.5;
        titlesSetting.lineHidden = NO;
        titlesSetting.spaceLineHidden = YES;
        titlesSetting.lineWidth = mScreenWidth/4;
        titlesSetting.textColor = [UIColor AxcUI_colorWithHexCode:@"#333333"];
        titlesSetting.selectedTextColor = [UIColor AxcUI_colorWithHexCode:@"#FC8963"];
        titlesSetting.lineBottomSpace = 0;
        titlesSetting.lineColor = [UIColor AxcUI_colorWithHexCode:@"#FC8963"];;
        //        titlesSetting.showRedPoint = YES;
        _titlesTopView = [[WSFSlideTitlesView alloc] initWithSetting:titlesSetting];
        _titlesTopView.delegate = self;
    }
    return _titlesTopView;
}
- (void)slideTitlesView:(WSFSlideTitlesView *)titlesView didSelectButton:(UIButton *)button atIndex:(NSUInteger)index{
    [self.dataArr removeAllObjects];
    [self loadMyCouponListWithType:[NSString stringWithFormat:@"%ld",index]];
}


@end
